using System;
using System.Collections.Generic;
using System.Reflection;

namespace NUtil {
    public class BeanIntrospector {
        public static List<PropertyInfo> GetPropertiesExclude(object obj, string[] propertiesToExclude) {
            if (null == obj) return new List<PropertyInfo>();
            List<PropertyInfo> properties = new List<PropertyInfo>(obj.GetType().GetProperties());
            foreach (string propertyName in propertiesToExclude) {
                PropertyInfo propertyToExclude = obj.GetType().GetProperty(propertyName);
                properties.Remove(propertyToExclude);
            }
            return properties;
        }

        public static List<PropertyInfo> GetPropertiesInclude(object obj, string[] propertiesToInclude) {
            if (null == obj) return new List<PropertyInfo>();
            List<PropertyInfo> properties = new List<PropertyInfo>();
            foreach (string propertyName in propertiesToInclude) {
                PropertyInfo property = obj.GetType().GetProperty(propertyName);
                if (property != null) {
                    properties.Add(property);
                }
            }
            return properties;
        }

        public static List<FieldInfo> GetFieldsExclude(object obj, string[] fieldNames) {
            if (null == obj) return new List<FieldInfo>();
            List<FieldInfo> fields =
                new List<FieldInfo>(obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic));
            foreach (string fieldName in fieldNames) {
                FieldInfo field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
                if (null != field) {
                    fields.Remove(field);
                }
            }
            return fields;
        }

        public static List<FieldInfo> GetFieldsInclude(object obj, string[] fieldNames) {
            if (null == obj) return new List<FieldInfo>();
            List<FieldInfo> fields = new List<FieldInfo>();
            foreach (String fieldName in fieldNames) {
                FieldInfo field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
                if (null != field) {
                    fields.Add(field);
                }
            }
            return fields;
        }
    }
}